package com.yx.cloud.view;

import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

import com.iot.product.sweeper.bean.Robot;
import com.iot.product.sweeper.bean.RobotStatus;
import com.iot.product.sweeper.config.SweeperProductConfig;
import com.yx.cloud.product.ProductConfigConstants;

import org.json.JSONArray;
import org.json.JSONObject;

public class LightAnimationUtils {
    private final static String TAG = "LightAnimationUtils";

    public static void setLight(ImageView view, Robot robot, boolean isFault) {
        view.clearAnimation();
        view.setVisibility(View.INVISIBLE);
        int state = robot.getRobotStatus().getWorkState();
        if (isFault) {
            //好像是x3s的特殊情况 没有错误状态 只有错误信息 也被当做错误状态处理
            state = RobotStatus.STATE_ERROR;
        }

        int mode = robot.getRobotStatus().getWorkMode();
        int battery = robot.getRobotStatus().getBattery();
        JSONObject productConfig =  robot.getConfig();

        JSONArray lightArray=productConfig.optJSONArray(ProductConfigConstants.robot_light_images);
        int drawable_id1 = lightArray.optInt(0);
        int drawable_id2 = lightArray.optInt(1);

        Log.d(TAG, "************  : " + state);

        if (state == RobotStatus.STATE_DORMANT) {
            //休眠 不显示
            lightOff(view);
        } else if (state == RobotStatus.STATE_STANDBY) {
            //待机
            if (battery < 20) {
                //粉-常亮
                ligthOn(view, drawable_id2);
            } else {
                //白-常亮
                ligthOn(view, drawable_id1);
            }
        } else if (state == RobotStatus.STATE_PAUSE) {
            //暂停
            if (battery >= 20) {
                //白-常亮
                ligthOn(view, drawable_id1);
            } else {
                //粉-常亮
                if (lightArray.length() >= 3) {
                    ligthOn(view, drawable_id2);
                } else {
                    lightFastFlash(view, drawable_id2);
                }
            }
        } else if (state == RobotStatus.STATE_WORKING) {
            //回充中
            if (mode == RobotStatus.MODE_RECHARGE) {
                if (battery < 20) {
                    //粉-闪烁
                    lightFastFlash(view, drawable_id2);
                } else {
                    //白持续闪烁
                    lightFastFlash(view, drawable_id1);
                }
            } else {
                //工作其他状态 白-常亮 除回充模式外 其他所有模式
                ligthOn(view, drawable_id1);
            }
        } else if (state == RobotStatus.STATE_GOTO_BS) {
            //返回基站中
            if (battery < 20) {
                //粉-常亮
                ligthOn(view, drawable_id2);
            } else {
                //白持续闪烁
                lightFastFlash(view, drawable_id1);
            }
        } else if (state == RobotStatus.STATE_CHARGING) {
            //充电状态
            if (battery == 100) {
                //白-常亮
                ligthOn(view, drawable_id1);
            } else {
                if (battery >= 20) {
                    //白持续呼吸
                    lightSlowFlash(view, drawable_id1);
                } else {
                    //粉持续呼吸
                    lightSlowFlash(view, drawable_id2);
                }
            }
        } else if (state == RobotStatus.STATE_CLEANING_MOP) {
            //白持续呼吸
            lightSlowFlash(view, drawable_id1);
        } else if (state == RobotStatus.STATE_ERROR) {
            //故障
            //红持续闪烁
            if (lightArray.length() >= 3) {
                int drawable_id3 = lightArray.optInt(2);
                lightFastFlash(view, drawable_id3);
            } else {
                lightFastFlash(view, drawable_id2);
            }

        } else {
            lightOff(view);
        }
    }

    private static void lightOff(View view) {
        view.clearAnimation();
        view.setVisibility(View.INVISIBLE);
    }

    private static void ligthOn(ImageView view, int resId) {
        view.setVisibility(View.VISIBLE);
        view.clearAnimation();
        view.setImageResource(resId);
    }

    private static void lightSlowFlash(ImageView view, int resId) {
        view.setVisibility(View.VISIBLE);
        view.setImageResource(resId);
        startAlphaAnimation(view, 1000);
    }


    private static void lightFastFlash(ImageView view, int resId) {
        view.setVisibility(View.VISIBLE);
        view.setImageResource(resId);
        startAlphaAnimation(view, 300);
    }

    private static void startAlphaAnimation(View view, long time) {
        view.clearAnimation();
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.2f, 1.0f);//渐变度从0到1
        alphaAnimation.setDuration(time);//动画持续时间：2000毫秒
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        view.startAnimation(alphaAnimation);
    }


}
